package cn.wolfcode.car.business.web.contorller;


import cn.wolfcode.car.business.domain.CarPackageAudit;
import cn.wolfcode.car.business.domain.ServiceItem;
import cn.wolfcode.car.business.qo.CarPackageAuditQuery;
import cn.wolfcode.car.business.service.ICarPackageAuditService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.web.AjaxResult;
import cn.wolfcode.car.shiro.ShiroUtils;
import org.apache.poi.util.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

/**
 * 岗位控制器
 */
@Controller
@RequestMapping("/business/carPackageAudit")
public class CarPackageAuditController {
    //模板前缀
    private static final String prefix = "business/carPackageAudit/";

    @Autowired
    private ICarPackageAuditService carPackageAuditService;

    //页面------------------------------------------------------------
    //列表
    @RequiresPermissions("system:carPackageAudit:view")
    @RequestMapping("/listPage")
    public String listPage() {
        return prefix + "list";
    }


    //数据-----------------------------------------------------------
    //列表
    @RequiresPermissions("system:carPackageAudit:list")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<CarPackageAudit> query(CarPackageAuditQuery qo) {
        return carPackageAuditService.query(qo);
    }


    //    processImg
    @RequestMapping("/processImg")
    @ResponseBody
    public AjaxResult processImg(Long id, HttpServletResponse response) {
        InputStream inputStream = carPackageAuditService.processImg(id);
        try {
            IOUtils.copy(inputStream, response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return AjaxResult.success();
    }

    @RequestMapping("/cancelApply")
    @ResponseBody
    public AjaxResult cancelApply(Long id) {
        carPackageAuditService.cancelApply(id);
        return AjaxResult.success();
    }


    @RequestMapping("/todoPage")
    public String todoPage() {
        return prefix+"todoPage";
    }

    @RequestMapping("/todoQuery")
    @ResponseBody
    public TablePageInfo<CarPackageAudit> todoQuery(CarPackageAuditQuery qo){
        //获取当前登录用户
        Long id = ShiroUtils.getUser().getId();
        qo.setId(id);
        //获取当前是否是审核状态
        qo.setAuditStatus(ServiceItem.AUDITSTATUS_AUDITING);
        return carPackageAuditService.query(qo);
    }

    @RequestMapping("/auditPage")
    public String auditPage(Long id,Model model){
        model.addAttribute("id",id);
        return prefix+"/auditPage";
    }

    @RequestMapping("/audit")//第一个参数是car的id,第二个参数是对或者错 错是1,第三个参数是备注
    @ResponseBody
    public AjaxResult audit(Long id,Long auditStatus,String info){
        carPackageAuditService.audit(id,auditStatus,info);
        return  AjaxResult.success();
    }

    @RequestMapping("/donePage")
    public String donePage(){
        return prefix+"/donePage";
    }

    @RequestMapping("/doneQuery")
    @ResponseBody
    public TablePageInfo<CarPackageAudit> doneQuery(CarPackageAuditQuery qo){
        String userName = ShiroUtils.getUser().getUserName();
        qo.setAuditStatus(CarPackageAudit.STATUS_PASS);
        qo.setUserName(userName);
        return carPackageAuditService.query(qo);
    }
}


